﻿ 
using System.Collections.Generic;
using System.Text; 
using UnityEngine;
using UnityEngine.Networking; 
using XFABManager; 
using XFGameFramework.Localization;


public class TransResult 
{
    public string src;
    public string dst;
}

public class BaiduTranslateResult 
{
    public string error_code;
    public string error_msg;

    public string from;
    public string to;

    public TransResult[] trans_result;

    private string _error;

    public string result
    {
        get {
            if (trans_result == null || trans_result.Length == 0)
                return string.Empty;
  
            return trans_result[0].dst;
        }
    }

    public bool isError
    {
        get 
        {
            return !string.IsNullOrEmpty(error_code) || !string.IsNullOrEmpty(error_msg);
        }
    }

    public string error
    {
        get 
        {
            if(string.IsNullOrEmpty(_error))
                _error = string.Format("error_code:{0} message:{1} 错误列表:https://fanyi-api.baidu.com/doc/21", error_code, error_msg);

            return _error;
        }
    }

}

public class YoudaoTranslateResult 
{
    public string errorCode;
    public string[] translation;

    private string _error;

    public string result
    {
        get
        {
            if (translation == null || translation.Length == 0)
                return string.Empty;

            return translation[0];
        }
    }

    public bool isError
    {
        get
        {
            return !errorCode.Equals("0");
        }
    }

    public string error
    {
        get
        {
            if (string.IsNullOrEmpty(_error))
                _error = string.Format("error_code:{0} 错误列表:https://ai.youdao.com/DOCSIRMA/html/trans/api/wbfy/index.html", errorCode);

            return _error;
        }
    }

}

public class TranslateController 
{

    private static string baidu_url = "https://fanyi-api.baidu.com/api/trans/vip/translate";

    private static readonly Dictionary<SystemLanguage, string> baidu_language = new Dictionary<SystemLanguage, string> 
    {
        { SystemLanguage.Afrikaans,"afr" }, { SystemLanguage.Arabic,"ara" }, { SystemLanguage.Basque,"baq" },{ SystemLanguage.Belarusian,"bel" },
        { SystemLanguage.Bulgarian,"bul" }, { SystemLanguage.Catalan,"cat" },{ SystemLanguage.Chinese,"zh" },{ SystemLanguage.Czech,"cs" },
        { SystemLanguage.Danish,"dan" },{ SystemLanguage.Dutch,"nl" },{ SystemLanguage.English,"en" },{ SystemLanguage.Estonian,"est" },
        { SystemLanguage.Faroese,"fao" }, { SystemLanguage.Finnish,"fin" }, { SystemLanguage.French,"fra" },{ SystemLanguage.German,"de" },
        { SystemLanguage.Greek,"el" },{ SystemLanguage.Hebrew,"heb" },{ SystemLanguage.Icelandic,"ice" },{ SystemLanguage.Indonesian,"id" },
        { SystemLanguage.Italian,"it" },{ SystemLanguage.Japanese,"jp" },{ SystemLanguage.Korean,"kor" },{ SystemLanguage.Latvian,"lav" },
        { SystemLanguage.Lithuanian,"lit" }, { SystemLanguage.Norwegian,"nor" },{ SystemLanguage.Polish,"pl" },{ SystemLanguage.Portuguese,"pt" },
        { SystemLanguage.Romanian,"rom" }, { SystemLanguage.Russian,"ru" }, { SystemLanguage.SerboCroatian,"hrv" },{ SystemLanguage.Slovak,"sk" },
        { SystemLanguage.Slovenian,"slo" },{ SystemLanguage.Spanish,"spa" },{ SystemLanguage.Swedish,"swe" },{ SystemLanguage.Thai,"th" },
        { SystemLanguage.Turkish,"tr" },{ SystemLanguage.Ukrainian,"ukr" },{ SystemLanguage.Vietnamese,"vie" },{ SystemLanguage.ChineseSimplified,"zh" },
        { SystemLanguage.ChineseTraditional,"cht" }, { SystemLanguage.Hungarian,"hu" },
    };
  

    private static UnityWebRequest Translate(  string from, string to, string content,int need_intervene = 1)
    {
        StringBuilder url = new StringBuilder();

        //if (string.IsNullOrEmpty(from))
        //    throw new System.Exception("源语言不能为空!");

        //if (string.IsNullOrEmpty(to))
        //    throw new System.Exception("目标语言不能为空!");

        //if (string.IsNullOrEmpty(content))
        //    throw new System.Exception("翻译内容不能为空!");

        
            string app_id = LocalizationSettings.Instance.baidu.app_id;
            string secret_key = LocalizationSettings.Instance.baidu.secret_key;

            string random = GenerateRandomNumber(10);

            string sign = XFABTools.md5(string.Format("{0}{1}{2}{3}", app_id, content, random, secret_key));
             
            url.Append(baidu_url).Append("?");
            url.Append("q=").Append( UnityWebRequest.EscapeURL(content)).Append("&");
            url.Append("from=").Append(from).Append("&");
            url.Append("to=").Append(to).Append("&");
            url.Append("appid=").Append(app_id).Append("&");
            url.Append("salt=").Append(random).Append("&");
            url.Append("sign=").Append(sign).Append("&");
            url.Append("needIntervene=").Append(need_intervene);

         
        //Debug.LogFormat("url:{0}",url);

        UnityWebRequest request = UnityWebRequest.Get(url.ToString());
        request.SendWebRequest();
        return request;
    }


    public static UnityWebRequest TranslateBaidu(SystemLanguage from,SystemLanguage to, string content, int need_intervene = 1) 
    {
        //if (!baidu_language.ContainsKey(from) || !baidu_language.ContainsKey(to))
        //    throw new System.Exception(string.Format("未查询到语言配置 {0} 或 {1} !",from,to));

        string f = string.Empty;
        if (baidu_language.ContainsKey(from))
            f = baidu_language[from];

        string t = string.Empty;
        if (baidu_language.ContainsKey(to))
            t = baidu_language[to];

        return Translate(f,t,content, need_intervene);
    }


    public static UnityWebRequest TranslateYoudao(SystemLanguage from, SystemLanguage to, string content, string vocabId = "") {

        return YoudaoTranslante.Translate(content, from, to, vocabId);
    }

    private static string GenerateRandomNumber(int count) 
    {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < count; i++)
        {
            s.Append(Random.Range(0, 10));
        }
        return s.ToString();
    }


}
